<?php

if(!defined('IN_JISHIGOU'))
{
exit('invalid request');
}
class ModuleObject extends MasterObject
{
function ModuleObject($config)
{
$this->MasterObject($config);
$this->Execute();
}
function Execute()
{
switch($this->Code)
{
case 'do_modify_setting':
{
$this->DoModifySetting();
break;
}
case 'modify':
{
$this->Modify();
break;
}
case 'do_modify':
{
$this->DoModify();
break;
}
case 'status0':
case 'status1':
{
$this->Status();
break;
}
case 'delete':
{
$this->Delete();
break;
}
case 'add':
{
$this->add();
break;
}
default :
{
$this->Main();
break;
}
}
}
function Main()
{
$api = $api_config = ConfigHandler::get('api');
if(!$api_config)
{
$api_config = array
(
'enable'=>0,
'from_enable'=>1,
);
ConfigHandler::set('api',$api_config);
}
Load::lib('form');
$query = $this->DatabaseHandler->Query("SELECT `id` , `title` , `pic` , left( `url` , 36 ) AS `url` , `category` 
FROM `jy_apps` ");
$app_list = array();
while(false != ($row = $query->GetRow()))
{
$apps_list[] = $row;
}
include($this->TemplateHandler->Template('admin/jyapi'));
}
function add(){
 $title = $this->Request['title'];
 //$pic = $this->Request['pic'];
 $url = $this->Request['url'];
 $category = $this->Request['category'];
 $update = date('Y-m-d');
 if(!empty($_FILES['pic']['name'])){
		 //$rnd = mt_rand(100, 999);
          $filename = date('YmdHis');
		  $savename=$filename.strtolower(strstr($_FILES['pic']['name'], '.'));
		  $s_pic= dirname(dirname(dirname(__FILE__)))."/images/api/".$savename;
		  if(is_uploaded_file($_FILES['pic']['tmp_name'])){
             if(!move_uploaded_file($_FILES['pic']['tmp_name'], $s_pic)){
             echo '移动文件失败！';
              exit;
          }
         }else{
           echo 'problem!';
           exit;
         }
	}
 if(!empty($title) && !empty($savename) && !empty($url)){
 $this->DatabaseHandler->Query("insert into ".TABLE_PREFIX."apps  values ('','$title','$savename','$url','$category','$update')");
 $query = $this->DatabaseHandler->Query("select * from ".TABLE_PREFIX."apps");
$app_list = array();
while(false != ($row = $query->GetRow()))
{
$apps_list[] = $row;
}
include($this->TemplateHandler->Template('admin/jyapi'));
 }else{
 include($this->TemplateHandler->Template('admin/jyapi_add'));
 }
}
function DoModifySetting()
{
$app_name_new = trim(strip_tags($this->Post['app_name_new']));
if($app_name_new)
{
$app_key = abs(crc32(md5(random(128))));
$app_secret = md5(random(128));
$status = 1;
$this->DatabaseHandler->Query("insert into ".TABLE_PREFIX."app (`uid`,`username`,`app_name`,`app_key`,`app_secret`,`status`) values ('".MEMBER_ID."','".MEMBER_NAME."','$app_name_new','$app_key','$app_secret','$status')");
$app_id = $this->DatabaseHandler->Insert_ID();
$this->Messager(null,"admin.php?mod=api&code=modify&id=$app_id");
}
$api = $this->Post['api'];
$api_config_default = $api_config = ConfigHandler::get('api');
$api_config['enable'] = ($api['enable'] ?1 : 0);
$api_config['from_enable'] = ($api['from_enable'] ?1 : 0);
$api_config['request_times_day_limit'] = (is_numeric($api['request_times_day_limit']) ?$api['request_times_day_limit'] : 0);
if($api_config_default != $api_config)
{
ConfigHandler::set('api',$api_config);
}
if($api_config['enable']!=$this->Config['api_enable'])
{
$config = array();
include(ROOT_PATH .'setting/settings.php');
$config['api_enable'] = $api_config['enable'];
ConfigHandler::set($config);
}
$this->Messager("修改成功");
}
function Modify()
{
$id = max(0,(int) $this->Request['id']);
if(!$id)
{
$this->Messager("请指定一个ID",null);
}
$apps = $this->DatabaseHandler->FetchFirst("select * from ".TABLE_PREFIX."apps where `id`='$id' ");
if(!$apps)
{
$this->Messager("请指定一个正确的ID",null);
}
Load::lib('form');

include($this->TemplateHandler->Template('admin/jyapi'));
}
function DoModify()
{
$id = max(0,(int) $this->Request['id']);
if(!$id)
{
$this->Messager("请指定一个ID",null);
}
$app = $this->DatabaseHandler->FetchFirst("select * from ".TABLE_PREFIX."apps where `id`='$id'");
if(!$app)
{
$this->Messager("请指定一个正确的ID",null);
}
$where ="";
$title = trim(strip_tags($this->Request['title']));
$url = trim(strip_tags($this->Request['url']));
$category = trim(strip_tags($this->Request['category']));
 if(!empty($_FILES['pic']['name'])){
		 //$rnd = mt_rand(100, 999);
          $filename = date('YmdHis');
		  $savename=$filename.strtolower(strstr($_FILES['pic']['name'], '.'));
		  $s_pic= dirname(dirname(dirname(__FILE__)))."/images/api/".$savename;
		  if(is_uploaded_file($_FILES['pic']['tmp_name'])){
             if(!move_uploaded_file($_FILES['pic']['tmp_name'], $s_pic)){
             echo '移动文件失败！';
              exit;
          }else{
		  $where = " `pic`='$savename', ";
		  }
         }else{
           echo 'problem!';
           exit;
         }
	}
$sql = "update ".TABLE_PREFIX."apps set `title`='$title',`url`='$url',".$where."`category`='$category' where `id`='$id'";
$this->DatabaseHandler->Query($sql);
$this->Messager("修改成功");
}
function Status()
{
$id = max(0,(int) $this->Request['id']);
if(!$id)
{
$this->Messager("请指定一个ID",null);
}
$app = $this->DatabaseHandler->FetchFirst("select * from ".TABLE_PREFIX."app where `id`='$id'");
if(!$app)
{
$this->Messager("请指定一个正确的ID",null);
}
$status = ('status0'==$this->Code ?0 : 1);
$this->DatabaseHandler->Query("update ".TABLE_PREFIX."app set `status`='$status' where `id`='$id'");
$this->Messager("设置成功");
}
function Delete()
{
$id = max(0,(int) $this->Request['id']);
if(!$id)
{
$this->Messager("请指定一个ID",null);
}
$app = $this->DatabaseHandler->FetchFirst("select * from ".TABLE_PREFIX."apps where `id`='$id'");
if(!$app)
{
$this->Messager("请指定一个正确的ID",null);
}
$this->DatabaseHandler->Query("delete from ".TABLE_PREFIX."apps where `id`='$id'");
$this->Messager("删除成功");
}
}
?>